//package com.caiyi.financial.nirvana.sms.scheduled;
//
//import com.alibaba.fastjson.JSON;
//import com.caiyi.financial.nirvana.sms.cache.SmsMateCache;
//import com.caiyi.financial.nirvana.sms.entity.SmsMate;
//import com.caiyi.financial.nirvana.sms.enums.SubscribeEnum;
//import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
//import com.caiyi.financial.nirvana.sms.jedis.JedisService;
//import com.caiyi.financial.nirvana.sms.service.SmsMateService;
//import com.caiyi.financial.nirvana.sms.service.SmsMsgService;
//import com.caiyi.financial.nirvana.sms.util.Constant;
//import com.caiyi.financial.nirvana.sms.util.HttpClientUtil;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.util.*;
//
//@Component
//public class UpdatePhoneProvinceTask {
//
//    private static final Logger logger = LoggerFactory.getLogger(UpdatePhoneProvinceTask.class);
//    @Autowired
//    private SmsMsgService smsMsgService;
//    @Autowired
//    private SmsMateService smsMateService;
//
//    /**
//     * 每十分钟更新sms_mate 省市为空的数据
//     */
//    @Scheduled(fixedRate = Constant.TEN_MINUTES_OF_SECOND * 1000, initialDelay = Constant.TEN_MINUTES_OF_SECOND * 1000)
//    public void updatePhoneToLocalAndData() {
//        try {
//            List<String> stringList = smsMsgService.getSmsByProvince();
//            Set<String> hashSet = new HashSet<>(stringList);
//            StringBuilder builder = new StringBuilder("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=");
//            List<SmsMate> smsMates = new ArrayList<>();
//            Map<String, String> map = new HashMap<>();
//            StringBuilder build = new StringBuilder();
//            for (String phone : hashSet) {
//                //不须重复插入sms_mate
//                if (JedisService.hget(JedisKey.SMS_MATE_VALUES, phone.substring(0, 7)) != null) {
//                    continue;
//                }
//                builder.append(phone);
//                String reStr = HttpClientUtil.doGet(builder.toString());
//                builder.delete(builder.length() - 11, builder.length());
//                if (reStr != null) {
//                    String[] split = reStr.split("\\{");
//                    String[] split1 = split[1].split("\\}");
//                    String[] values = split1[0].split(",");
//                    if (values != null && values.length >= 7) {
//                        String[] phoneHeads = values[0].split(":");
//                        String phoneHeads_value = phoneHeads[1].substring(1, phoneHeads[1].length() - 1);
//                        String[] province = values[1].split(":");
//                        String province_value = province[1].substring(1, province[1].length() - 1);
//                        String[] catName = values[2].split(":");
//                        String types = catName[1].substring(1, catName[1].length() - 1);
//                        String[] carrier = values[6].split(":");
//                        String isp = carrier[1].substring(1, carrier[1].length() - 2);
//
//                        //更新sms_msg
//                        smsMsgService.setProvinceAndCityByPhone(province_value, province_value, phone);
//
//                        if (map.containsKey(phoneHeads_value)) {
//                            continue;
//                        }
//                        //入库sms_mate
//                        SmsMate smsMate = new SmsMate();
//                        smsMate.setCreateTime(new Date());
//                        smsMate.setIsp(isp);
//                        smsMate.setPhone(phoneHeads_value);
//                        smsMate.setTypes(types);
//                        smsMate.setProvince(province_value);
//                        smsMate.setCity(province_value);
//                        smsMates.add(smsMate);
//
//                        //更新redis
//                        build.append(province_value).append(",").append(province_value);
//                        map.put(smsMate.getPhone(), build.toString());
//                        build.delete(0, build.length());
//
//                    } else {
//                        logger.info("未匹配上的电话：{}", phone);
//                        smsMsgService.setProvinceAndCityByPhone("未知", "未知", phone);
//                    }
//                }
//            }
//            if (!smsMates.isEmpty()) {
//                smsMateService.insertList(smsMates);
//            }
//            if (!map.isEmpty()) {
//                JedisService.hmset(JedisKey.SMS_MATE_VALUES, map);
//                //更新内存
//                map.put("method", SubscribeEnum.ADDSMSMATETOLOCAL.method);
//                JedisService.publish(JedisKey.SMS_SUBSCRIBE_API_ACTION, JSON.toJSONString(map));
//                SmsMateCache.addSmsMateToLocal(map);
//            }
//        } catch (Exception e) {
//            logger.error("UpdatePhoneProvinceTask 更改数据 error", e);
//        }
//    }
//
//}
